abs
method abs(x: int) returns (y: int)
ensures true
{
if x < 0 {
y := -x;
} else {
y := x;
}
}
method abs(x: int) returns (y: int):method: This keyword indicates the definition of a method. In Dafny, methods are used to define procedures that can take parameters, execute code, and return values.
abs: The name of the method, which computes the absolute value of the input x.
(x: int): A parameter x of type int (integer) that the method will accept.
returns (y: int): Indicates that the method will return a value named y, which is also of type int.
ensures true:ensures: This keyword specifies a postcondition that must hold true when the method completes. Here, true indicates that there are no specific conditions that need to be met upon method completion.if x < 0 {:x is less than 0.y := -x;:y := -x;: This line assigns the negation of x to y if x is negative, effectively computing the absolute value.else {:x is not negative.y := x;:x is not negative, y is assigned the value of x, meaning the absolute value of a non-negative number is the number itself.foo
method foo(x: int)
requires x >= 0
{
var y := abs(x);
// assert( y == x);
}
method foo(x: int):foo and takes one parameter x of type int.requires x >= 0:requires: This keyword introduces a precondition that must be satisfied before the method can be executed. In this case, x must be greater than or equal to 0.var y := abs(x);:y and assigns it the result of calling the abs method with x as the argument.// assert( y == x);:y equals x. Since x is guaranteed to be non-negative, this assertion would hold true if the abs method is functioning correctly.max
method max(x: int, y: int) returns (m: int)
requires true;
ensures true;
{
var r : int;
if x > y {
r := 0;
} else {
r := 1;
}
m := r;
// return r;
// return m;
}
method max(x: int, y: int) returns (m: int):max that takes two integers x and y and returns an integer m.requires true;:ensures true;:var r : int;:r of type int.if x > y {:x is greater than y.r := 0;:x is greater than y, assigns 0 to r.else { r := 1; }:x is not greater than y, assigns 1 to r.m := r;:r to the return variable m. Note: This is incorrect logic for finding the maximum; it should instead return x or y.ex1
method ex1(n: int)
requires true
ensures true
{
var i := 0;
while i < n
invariant true
// decreases *
{
i := i + 1;
}
// assert i == n;
}
method ex1(n: int):ex1 that takes an integer n.requires true:ensures true:var i := 0;:i to 0.while i < n:i is less than n.invariant true:invariant: A condition that must hold true before and after each iteration of the loop. Here, it is always true, which means it doesn’t provide any useful information.// decreases *:decreases: A clause that indicates a measure that decreases with each iteration of the loop, ensuring termination. In this case, it is left blank, which is incorrect for proving termination.i := i + 1;:i by 1 on each iteration.// assert i == n;:i should equal n.foo2
method foo2()
ensures false
decreases *
{
while true
decreases *
{
}
assert false;
}
method foo2():foo2 that takes no parameters.ensures false:decreases *:while true:assert false;:find
method find(a: seq<int>, key: int) returns (index: int)
requires true
ensures true
{
index := 0;
while index < |a|
invariant true
{
if a[index] == key {
return 0;
}
index := index + 2;
}
index := -10;
}
method find(a: seq<int>, key: int) returns (index: int):find that takes a sequence of integers a and an integer key, returning an integer index.requires true:ensures true:index := 0;:index to 0.while index < |a|:index is less than the length of sequence a.invariant true:if a[index] == key { return 0; }:index in a equals key, the method returns 0, which is incorrect as it should return the index of the key.index := index + 2;:index by 2, skipping every other element.index := -10;:index to -10, which is not a meaningful result for the search.isPalindrome
method isPalindrome(a: seq<char>) returns (b
: bool)
{
return true;
}
method isPalindrome(a: seq<char>) returns (b: bool):isPalindrome that takes a sequence of characters a and returns a boolean b.return true;:true, without any logic to check for a palindrome.sorted
predicate sorted(a: seq<int>)
{
forall j, k::0 <= j < k < |a| ==> a[j] <= a[k]
}
predicate sorted(a: seq<int>):sorted that checks if a sequence of integers a is sorted in ascending order.forall j, k::0 <= j < k < |a| ==> a[j] <= a[k]:j and k within the valid range of the sequence, if j is less than k, then the value at j must be less than or equal to the value at k.unique
method unique(a: seq<int>) returns (b: seq<int>)
requires sorted(a)
ensures true
{
return a;
}
method unique(a: seq<int>) returns (b: seq<int>):unique that takes a sorted sequence of integers a and returns a sequence b.requires sorted(a):ensures true:return a;:a without removing duplicates.Main
method Main() {
var r := find([], 1);
print r, "\n";
r := find([0,3,5,7], 5);
print r, "\n";
var s1 := ['a'];
var r1 := isPalindrome(s1);
print "is [", s1, "]", " a isPalindrome? ", r1, " \n";
s1 := [];
r1 := isPalindrome(s1);
print "is [", s1, "]", " a isPalindrome? ", r1, " \n";
s1 := ['a', 'b'];
r1 := isPalindrome(s1);
print "is [", s1, "]", " a isPalindrome? ", r1, " \n";
s1 := ['a', 'b', 'a'];
r1 := isPalindrome(s1);
print "is [", s1, "]", " a isPalindrome? ", r1, " \n";
var i := [0,1,3,3,5,5,7];
var s := unique(i);
print "unique applied to ", i, " is ", s, "\n";
}
method Main() {:var r := find([], 1);:find method with an empty sequence and prints the result.print r, "\n";:find method followed by a newline.isPalindrome:isPalindrome method with different character sequences, printing the results each time.var i := [0,1,3,3,5,5,7];:i with some duplicates.var s := unique(i);:unique method to remove duplicates from the sequence.print "unique applied to ", i, " is ", s, "\n";:unique method to the sequence i.Method: A named sequence of instructions that can take inputs and return outputs.
Ensures: Specifies the conditions that must hold true after the method execution.
Requires: Specifies the conditions that must be true before executing the method.
Invariant: A condition that holds true before and after each iteration of a loop.
Decreases: A clause used to ensure that a loop will eventually terminate by indicating a measure that decreases with each iteration.
Predicate: A function that returns a boolean value, typically used to specify properties of data.
Return: The output of a method, indicated by the returns keyword.